//
//  YJCountdownTextView.m
//  CToolBox
//
//  Created by crj on 2017/8/8.
//  Copyright © 2017年 SC. All rights reserved.
//

#import "YJCountdownTextView.h"

@interface YJCountdownTextView()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *countdownLabel;

@end

@implementation YJCountdownTextView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self initConfig];
        [self makeUI];
    }
    return self;
}

- (void)initConfig{
    
    _maxCount = 1000;
    _textColor = [UIColor grayColor];
    _countdownLabelColor = [UIColor lightGrayColor];
    _textSize = 15;
    _countdownSize = 12;
    _textSpaceV = 10;
}

- (void)makeUI{

    [self addSubview:self.textView];
    [self addSubview:self.countdownLabel];
}

- (void)layoutSubviews{

    [super layoutSubviews];
    CGFloat selfWidth = self.frame.size.width;
    CGFloat selfHeight = self.frame.size.height;
    CGFloat space = 10;
    CGFloat topAndBottomSpace = 20;
    CGFloat countdownLabelheight = 45;
    
    _textView.frame = CGRectMake(space, topAndBottomSpace, selfWidth-space*2, selfHeight-countdownLabelheight-topAndBottomSpace);
    _countdownLabel.frame = CGRectMake(space, _textView.frame.origin.y+_textView.frame.size.height, _textView.frame.size.width, countdownLabelheight);
}

- (UITextView *)textView{

    if(!_textView){
    
        _textView = [[UITextView alloc] init];
        _textView.delegate = self;
        _textView.textColor = _textColor;
        _textView.font = [UIFont systemFontOfSize:_textSize];
    }
    return _textView;
}

- (UILabel *)countdownLabel{

    if(!_countdownLabel){
        _countdownLabel = [[UILabel alloc] init];
        _countdownLabel.text = [NSString stringWithFormat:@"还能输入%ld字",_maxCount];
        _countdownLabel.textColor = _countdownLabelColor;
        _countdownLabel.font = [UIFont systemFontOfSize:_countdownSize];
        _countdownLabel.textAlignment = NSTextAlignmentRight;
    }
    return _countdownLabel;
}

#pragma mark - TextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    // 判断是否有候选字符，如果不为nil，代表有候选字符
    if(textView.markedTextRange == nil){
        NSString *text = textView.text;
        _textView.attributedText = [self attributedStringWithLineSpace:_textSpaceV string:text];
        _countdownLabel.text = [NSString stringWithFormat:@"还能输入%ld字",_maxCount - text.length];
    }
    self.text = textView.text;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if(textView.text.length>= _maxCount){
        if(![text isEqualToString:@""]){//字数大于最大数后，除退格键外都不能输入
            return NO;
        }
    }
    
    return YES;
}

#pragma mark - Set


- (void)setTextColor:(UIColor *)textColor{

    _textColor = textColor;
    self.textView.textColor = _textColor;
}

- (void)setCountdownLabelColor:(UIColor *)countdownLabelColor{

    _countdownLabelColor = countdownLabelColor;
    self.countdownLabel.textColor = _countdownLabelColor;
}

- (void)setTextSize:(CGFloat)textSize{

    _textSize = textSize;
    self.textView.font = [UIFont systemFontOfSize:_textSize];
}

- (void)setCountdownSize:(CGFloat)countdownSize{

    _countdownSize = countdownSize;
    self.countdownLabel.font = [UIFont systemFontOfSize:_countdownSize];
}

- (void)setTextSpaceV:(CGFloat)textSpaceV{
    
    _textSpaceV = textSpaceV;
    self.textView.attributedText = [self attributedStringWithLineSpace:_textSpaceV string:_textView.text];
}

- (void)setMaxCount:(long)maxCount{

    _maxCount = maxCount;
    self.countdownLabel.text = [NSString stringWithFormat:@"还能输入%ld字",_maxCount-self.countdownLabel.text.length];
}

#pragma mark - Private


- (NSMutableAttributedString *)attributedStringWithLineSpace:(CGFloat)lineSpace string:(NSString *)string{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    [paragraphStyle setLineSpacing:lineSpace];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:_textSize] range:NSMakeRange(0, [string length])];
    return attributedString;
}




@end
